package
{
	import fl.containers.ScrollPane;
	import fl.controls.Button;
	import fl.controls.TextArea;
	import fl.controls.TextInput;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.IDataInput;
	import flash.utils.IDataOutput;
	
	import mx.utils.StringUtil;
	
	import org.musince.global.$log;
	import org.musince.logic.ISerializable;
	
	
	[SWF(width="1280", height="720", backgroundColor="0xFFFFFF")]
	
	public class FirstIOS extends Sprite
	{
		public var sroot:Sprite = new Sprite();
		public var rows:Array = new Array();
			
		public var scroll:ScrollPane = new ScrollPane();
		
		public function FirstIOS()
		{
			var ta:TextArea = new TextArea();
			$log = new TALog(ta);
			var rpcs:Array = $protocol.rpcs;
			rows = new Array(rpcs.length);
			var row:Sprite;
			for (var i:int = 0; i < rpcs.length; i++)
			{
				rows[i] = row = new Sprite();
				row.y = i * 40;
				addRPC(rpcs[i], row, i);
				sroot.addChild(row);
			}
			scroll.setSize(1280, 420);
			scroll.source = sroot;
//			addChild(sroot);
			addChild(scroll);
			ta.setSize(1280, 300);
			ta.y = 720-ta.height;
			addChild(ta);
		}
		
		public function addRPC(rpc:Object, row:Sprite, i:int):void {
			var args:Array = rpc.args;
			var label:TextField;
			var input:TextInput 
			var button:Button = new Button();
			button.name = i.toString();
			button.label = rpc.name;			
			button.addEventListener(MouseEvent.CLICK, onClick);
			row.addChild(button);
			var x:int = button.width + 5;
			for each(var arg:Object in args) {
				label = new TextField();
				label.text = arg.label;
				label.autoSize = TextFieldAutoSize.LEFT;
				label.x = x;
				x += label.width;
				row.addChild(label);			
				input = new TextInput();
				input.x = x;
				x += input.width + 5;
				row.addChild(input);
			}
		}
		
		public var querying:ShortQuery;
		
		public function onClick(e:Event):void
		{
			var i:int = int(e.currentTarget.name);
			var rpc:Object = $protocol.rpcs[i];
			if (querying != null) return;
			querying = new ShortQuery();
			var row:Sprite = rows[i];
			var args:Array = rpc.args;
			var arg:Object;
			var buf:IDataOutput = querying.prepare(rpc.name, args.length);
			var v:String;
			var str:String;
			var strArr:Array; 
			for (var j:int = 0; j < args.length; j++)
			{
				arg = args[j];
				v = (row.getChildAt(2+j*2) as TextInput).text;
				switch (arg.type) {
					case $protocol.TYPE_BYTE:
						$protocol.writeByte(buf, int(v));
						break;
					case $protocol.TYPE_INT:
						$protocol.writeInt(buf, int(v));
						break;
					case $protocol.TYPE_STRING:
						$protocol.writeString(buf, v);
						break;
					case $protocol.TYPE_OBJECT:
						try {
							serializeJSON(buf, arg.cls, v);
						} catch (err:Error) {
							querying = null;
							$log.error(arg.cls+"format error");
							return;
						}
						break;
					case $protocol.TYPE_INT_ARRAY:
						strArr = StringUtil.trim(v).split(",");
						buf.writeByte($protocol.TYPE_INT_ARRAY);
						buf.writeInt(strArr.length);
						for each (str in strArr) {
							buf.writeInt(int(str));
						}	
						break;
					case $protocol.TYPE_STRING_ARRAY:
						strArr = StringUtil.trim(v).split(",");
						buf.writeByte($protocol.TYPE_STRING_ARRAY);
						buf.writeInt(strArr.length);
						for each (str in strArr) {
						buf.writeUTF(str);
					}
						break;
				}
			}
			querying.onError = this.onServerError;
			if (this.hasOwnProperty(rpc.name+"Resp")) {
				querying.send(this[rpc.name+"Resp"]);
			} else {
				querying.send(defaultQueryCallback);
			}
		}
		
		public function defaultQueryCallback(data:IDataInput):void {
			querying = null;
		}
		
		public function serializeJSON(buf:IDataOutput, cls:Class, str:String):void
		{
			var json:Object = JSON.parse(str);
			var ser:ISerializable = new cls();
			for (var key:Object in json) {
				ser[key] = json[key];
			}	
			$protocol.writeObject(buf, ser);
		}
		
		public function onServerError(q:ShortQuery):void
		{
			$log.debug("server error");
			querying = null;
		}
		
		public function registerAccountResp(data:IDataInput):void {
			var success:int = data.readByte();
			if (success == 1) {
				$log.debug("register success");
			} else {
				$log.debug("register failed");
			}
			 
			querying = null;
		}
		
		public function updatePlayerResp(data:IDataInput):void {
			var ranking:int = data.readInt();
			$log.debug("receive ranking("+ranking+")");
			querying = null;	
		}
				
		public function challengeResultResp(data:IDataInput):void {
			var score:int = data.readInt();
			var ranking:int = data.readInt();
			var percent:int = data.readInt();
			$log.debug("receive ("+score+") ranking("+ranking+") percent("+percent);
			querying = null;
		}
		
		public function getPVPRankResp(data:IDataInput):void {
			$log.debug("receive pvp rank ");
			var count:int = data.readInt();
			var guid:String; var name:String; var level:int; 
			var rank:int; var score:int; var sex:int; var percent:int;
			for (var i:int = 0; i < count; i++) {
				guid = data.readUTF();
				sex = data.readByte();
				name = data.readUTF();
				level = data.readInt();
				score = data.readInt();
				rank = data.readInt();
				percent = data.readInt();
				$log.debug("[PVP Rank] name("+name+") guid("+guid+") level("
					+level+") rank("+rank+") score("+score+") sex("+sex+") percent("+percent+")");
			}
			querying = null;
		}
		
		public function getChallengeListResp(data:IDataInput):void {
			$log.debug("receive challenge list");
			var guid:String; var name:String; var level:int; 
			var rank:int; var score:int; var sex:int; var percent:int;
			var empty:int;
			var player:DataPlayer = new DataPlayer();
			for (var i:int = 0; i < 12; i++) {
				empty = data.readByte();
				if (empty == 0) continue;
				guid = data.readUTF();
				sex = data.readByte();
				name = data.readUTF();
				level = data.readInt();
				score = data.readInt();
				rank = data.readInt();
				percent = data.readInt();
				$log.debug("[Challenge List] name("+name+") guid("+guid+") level("
					+level+") rank("+rank+") score("+score+") sex("+sex+") percent("+percent+")");
			}
			querying = null;
		}
		
		public function getBattlePlayerResp(data:IDataInput):void {
			$log.debug("battle player list");
			querying = null;
		}
		
		public function getChallengeRecordResp(data:IDataInput):void {
			$log.debug("challenge  record");
			var count:int = data.readInt();
			var name:String; var guid:String; var level:int; 
			var sex:int; var win:int;
			for (var i:int = 0; i < count; i++) {
				name = data.readUTF();
				guid = data.readUTF();
				level = data.readInt();
				sex = data.readByte();
				win = data.readByte();
				$log.debug("[Challenge Record] name("+name+") guid("+guid+") level("+level+") sex("+sex+") win("+win+")");
			}
			querying = null;
		}

	}
}